<template>
	<view class="container">
		<view class="tui-box">
			<tui-list-cell :arrow="true" unlined :radius="true">
				<view class="tui-address">
					<view v-if="pickupaddress">
						<view>
							<view class="tui-userinfo">
								<text class="tui-name">{{pickupaddress.address}}</text>{{pickupaddress.phone}}
							</view>
							<view class="tui-addr">

								<text>自提点:{{pickupaddress.name}}</text>
							</view>
						</view>
					</view>
					<view v-else>
						<view v-if="address" @click="addressList">
							<view class="tui-userinfo">
								<text class="tui-name">{{address.addressee}}</text>{{address.mobile}}
							</view>
							<view class="tui-addr">
								<view class="tui-addr-tag">家</view>
								<text>{{address.address}}</text>
							</view>
						</view>
						<view class="tui-none-addr" v-else @click="chooseAddr">
							<image src="/static/images/index/map.png" class="tui-addr-img" mode="widthFix"></image>
							<text>选择收货地址</text>
						</view>
					</view>

				</view>
				<view class="tui-bg-img"></view>
			</tui-list-cell>
			<view class="tui-top tui-goods-info">
				<tui-list-cell :hover="false" :lineLeft="false">
					<view class="tui-goods-title">
						商品信息
					</view>
				</tui-list-cell>
				<view v-if="cart==true">
					<block v-for="(item,index) in products.order_goods_list" :key="index">
						<tui-list-cell :hover="false" padding="0">
							<view class="tui-goods-item">
								<image :src="item.sku_list[0].picture_info.pic_cover_small" class="tui-goods-img">
								</image>
								<view class="tui-goods-center">
									<view class="tui-goods-name">{{item.goods_name}}</view>
									<view class="tui-goods-attr">{{item.sku_name}}</view>
								</view>
								<view class="tui-price-right">
									<view>￥{{item.promote_price}}</view>
									<view>x{{item.num}}</view>
								</view>
							</view>
						</tui-list-cell>
					</block>
				</view>
				<view v-else>
					<block v-for="(item,index) in products.order_goods_list" :key="index">
						<tui-list-cell :hover="false" padding="0">
							<view class="tui-goods-item">
								<image :src="item.pic_cover_small" class="tui-goods-img">
								</image>
								<view class="tui-goods-center">
									<view class="tui-goods-name">{{item.goods_name}}</view>
									<view class="tui-goods-attr">{{item.sku_name}}</view>
								</view>
								<view class="tui-price-right">
									<view>￥{{item.promote_price}}</view>
									<view>x{{item.buy_num}}</view>
								</view>
							</view>
						</tui-list-cell>
					</block>
				</view>

				<tui-list-cell :hover="false" v-if="products.point_exchange_type>=1">
					<view class="tui-padding tui-flex">
						<view>抵扣券总额</view>
						<view class="coin-nums">{{products.point_exchange}}抵扣券</view>
					</view>
				</tui-list-cell>
				<tui-list-cell :hover="false">
					<view class="tui-padding tui-flex">
						<view>商品总额</view>
						<view>￥{{products.total_goods_price}}</view>
					</view>
				</tui-list-cell>
				<!-- <tui-list-cell :arrow="hasCoupon" :hover="hasCoupon">
					<view class="tui-padding tui-flex">
						<view>优惠券</view>
						<view :class="{'tui-color-red':hasCoupon}">{{hasCoupon?"满5减1":'没有可用优惠券'}}</view>
					</view>
				</tui-list-cell> -->
				<tui-list-cell :hover="false">
					<view class="tui-padding tui-flex">
						<view>配送费</view>
						<view>￥{{products.total_express_fee}}</view>
					</view>
				</tui-list-cell>
				<tui-list-cell :hover="false" :lineLeft="false" padding="0">
					<view class="tui-remark-box tui-padding tui-flex">
						<view>订单备注</view>
						<input type="text" class="tui-remark" placeholder="选填: 请先和商家协商一致"
							placeholder-class="tui-phcolor"></input>
					</view>
				</tui-list-cell>
				<tui-list-cell :hover="false" unlined>
					<view class="tui-padding tui-flex tui-total-flex">
						<view class="tui-flex-end tui-color-red">
							<view class="tui-black">合计： </view>
							<view class="tui-size-26">￥</view>
							<view class="tui-price-large">{{totalPrice}}</view>
							<!-- <view class="tui-size-26">.00</view> -->
						</view>
					</view>
				</tui-list-cell>
			</view>
		</view>
		<view class="tui-safe-area"></view>
		<view class="tui-tabbar">
			<view class="tui-flex-end tui-color-red tui-pr-20">
				<view class="tui-black">实付金额: </view>
				<view class="tui-size-26">￥</view>
				<view class="tui-price-large">{{totalPrice}}</view>
				<!-- <view class="tui-size-26">.00</view> -->
			</view>
			<view class="tui-pr25">
				<tui-button width="200rpx" height="70rpx" :size="28" type="warning" shape="circle"
					@click="btnPay">确认支付</tui-button>
			</view>
		</view>
		<t-pay-way :show="show" :shop_id="shop_id" :paypassword="paypassword" @keyboard="switchKeyboard(6)"
			@close="popupClose" :total_price="totalPrice" :balance="balance" :team_balance="team_balance"
			:out_trade_no="out_trade_no" :iswx="iswx" :isth="isth"></t-pay-way>
		<tui-keyboards :radius="radius" :shows="shows" @close="closeKeyboard" @click="keyboardClick">
			<view class="tui-digital-box">
				<view class="tui-keyboard-tips">请输入密码</view>
				<tui-keyboard-input :inputvalue="pwdArr"></tui-keyboard-input>
			</view>
		</tui-keyboards>
	</view>
</template>

<script>
	import tPayWay from "@/components/views/t-pay-way/ti-pay-way"
	export default {
		components: {
			tPayWay
		},
		data() {
			return {
				hasCoupon: true,
				insufficient: false,
				show: false,
				shows: false,
				shop_id: 0,
				shopsMsg: '',
				address: 0,
				addresss: [],
				products: [{
					pic_cover_small: ''
				}],
				buyer_message: '',
				address_id: '',
				pickupaddress: '',
				totalFee: '',
				totalPrice: '',
				defaultAddress: '0',
				balance: '',
				isDoRefresh: '',
				shipping_fee: '',
				method: '',
				out_trade_no: '',
				team_balance: 0,
				cart: false,
				cart_arr: '',
				total_delivery_money: '',
				total_xfmoney: '',
				iswx: '',
				isth: '',
				numberArr: [],
				pwdArr: ["", "", "", "", "", ""],
				temp: ["", "", "", "", "", ""],
				radius: false,
				shipping_type: 1,
				paypassword: '',
				is_password: {
					type: Boolean,
					default: false
				},
				pickup_id: 0,
			}
		},
		onShow: function(e) {
			if (this.isDoRefresh == true) {
				this.isDoRefresh = false;
				this.init();

			}

		},
		onLoad(options) {

			this.tui.haslogin();
			let pages = getCurrentPages();
			let currPage = pages[pages.length - 1];
			if (currPage.$vm.shopsMsg) {
				this.shopsMsg = JSON.parse(currPage.$vm.shopsMsg);
				if (this.shopsMsg.cart) {
					this.cart = true;
				}
				this.init()
			} else {
				if (options.shopsMsg) {
					if (this.tui.isLogin()) {
						this.shopsMsgs = options.shopsMsg
						this.shopsMsg = JSON.parse(options.shopsMsg);
						if (this.shopsMsg.cart) {
							this.cart = true;
						}
						this.init()
					}

				}
			}

			// if (options.shopsMsg) {
			// 	if (this.tui.isLogin()) {
			// 		this.shopsMsg = JSON.parse(options.shopsMsg);
			// 		if (this.shopsMsg.cart) {
			// 			this.cart = true;
			// 		}
			// 		if (this.shopsMsg.pickup_id) {
			// 			/* 获取自提点 */
			// 			this.pickupInfo(this.shopsMsg.pickup_id);
			// 			this.shipping_type = 2;
			// 			this.pickup_id = this.shopsMsg.pickup_id
			// 		}

			// 	}

			// }


		},
		methods: {
			setData(isDoRefresh, isAddressId) {
				this.isDoRefresh = true;
				this.isAddressId = isAddressId
			},
			chooseAddr() {

				uni.navigateTo({
					url: "/pageschild/editAddress/editAddress?shopsMsg=" + this.shopsMsgs
				})
			},
			addressList() {
				uni.navigateTo({
					url: "/pageschild/address/address?shopsMsg=" + this.shopsMsgs + "&msg=" + 1
				})
			},
			init() {
				let token = this.tui.getToken();
				if (!this.pickup_id) {
					let data = {
						token: token,
					};
					let datas = {
						token: token,
						is_default: this.tui.getAddressId() ? '' : 1,
						address_id: this.tui.getAddressId()
					};
					// #ifdef H5
					// let ua = window.navigator.userAgent.toLowerCase()
					// if (ua.match(/MicroMessenger/i) == 'micromessenger') {
					// 	this.iswx = true;
					// }
					//#endif
					let addresss = this.tui.request('user', 'post', data, 1000, 1, '', 'address');
					let address = this.tui.request('user', 'post', datas, 1000, 1, '', 'address');


					Promise.resolve(address).then((value) => {
						if (value.code == 200) {
							this.address = value.data;
							// console.log(this.address)
							if (value.data) {

								this.defaultAddress = value.data

							}
						} else {
							Promise.resolve(addresss).then((value) => {
								if (value.code == 200) {
									this.addresss = value.data;
									// console.log(this.address)
									if (value.data) {
										this.address = this.addresss[0];
										this.defaultAddress = this.addresss[0]
									}
								}
							})
						}
					})
				}

				let account = this.tui.request('user', 'post', {
					token: token
				}, 1000, 1, '', 'userAccount');
				Promise.resolve(account).then((value) => {
					if (value.code == 200) {
						this.balance = value.data.balance;
					} else {
						this.tui.toast(value.message);
					}
				})
				let obj = {
					token: token,
				}

				let gid_num_sku = this.shopsMsg.gid_num_sku.split(":");
				obj.buy_info = gid_num_sku[0] + ":" + gid_num_sku[1]
				obj.shipping_type = this.shipping_type
				this.method = 'buyNow';


				let orders = this.tui.request('order', 'post', obj, 1000, 1, '', this.method);
				Promise.resolve(orders).then((value) => {
					if (value.code == 200) {
						this.products = value.data;
						if (this.shopsMsg.cart) {
							this.totalPrice = this.products.total_order_price;
							this.shipping_fee = this.products.total_express_fee;
							this.products = value.data.data;

						} else {
							this.totalPrice =parseFloat(this.products.total_order_price);
							this.shipping_fee = this.products.total_express_fee;
							this.total_express_fee = this.products.total_express_fee;
							if (this.products.point_exchange_type == 1) {
								this.totalFee = this.products.price + parseFloat(this.products.total_express_fee);
							}
							this.shop_id = this.products.shop_id
						}
					} else {
						this.tui.toast(value.message);
					}
				})
			},
			switchKeyboard(index) {

				let length = index; //e.currentTarget.dataset.length;
				let arr = ["", "", "", "", "", ""]
				let radius = false

				if (length == 4) {
					arr = ["", "", "", ""]
					radius = true
				}
				this.pwdArr = arr;
				this.temp = arr;
				this.numberArr = [];
				this.radius = radius;

				this.shows = true;

			},
			closeKeyboard: function() {
				this.shows = false;
				this.numberArr = [];
				this.pwdArr = this.temp
			},
			pickupInfo(pickup_id) {
				let token = this.tui.getToken();
				let datas = {
					token: token,
					pickUpId: pickup_id
				};
				let pickid = this.tui.request('shop', 'post', datas, 1000, 1, '', 'pickUpInfo');
				Promise.resolve(pickid).then((value) => {
					if (value.code == 200) {
						this.pickupaddress = value.data;
					}
				})
			},
			getPwd: function() {
				//判断并取出密码

				if (this.numberArr.length === this.pwdArr.length) {
					uni.showLoading({
						title: '支付订单',
						mask: true
					})
					setTimeout(() => {
						let pwd = this.numberArr.join('')
						this.closeKeyboard();
						this.paypassword = pwd;
						let token = this.tui.getToken();
						var pays = {
							token: token,
							pay_type: 3,
							out_trade_no: this.out_trade_no,
							pay_app: 'wap',
							pay_password: this.paypassword
						}

						let paytype = this.tui.request('order', 'post', pays, 1000, 1, '', 'createOrder');
						Promise.resolve(paytype).then((value) => {
							if (value.code == 200) {
								uni.navigateTo({
									url: "../success/success?out_trade_no=" + this.out_trade_no
								})
							} else {

								this.tui.toast(value.message);
							}
						})

					}, 800);
				}
			},
			keyboardClick: function(e) {
				let numberArr = this.numberArr;
				let pwdArr = this.pwdArr;
				let index = e.index;
				if (numberArr.length === pwdArr.length || index == undefined) {
					return;
				}
				if (index == 9) { //取消键
					this.closeKeyboard();
					this.noClick = false;
					return;
				} else if (index == 11) {
					//退格键
					let len = numberArr.length;
					if (len) {
						pwdArr.splice(len - 1, 1, "");
					} else {
						pwdArr = this.temp;
					}
					numberArr.pop()
				} else if (index == 10) {
					numberArr.push(0);
					pwdArr.splice(numberArr.length - 1, 1, "●");
				} else {
					numberArr.push(index + 1);
					pwdArr.splice(numberArr.length - 1, 1, "●");
				}
				this.numberArr = numberArr;
				this.pwdArr = pwdArr;
				this.getPwd();
			},
			btnPay() {
				if (!this.defaultAddress.id && !this.pickup_id) {
					this.tui.toast('请先添加地址');
					return;
				}
				//先申请下单数据
				let token = this.tui.getToken();
				let obj = {
					token: token,
				}
				if (this.shopsMsg.cart) {
					this.cart_arr = this.cart_arr.toString()
					obj.cart_ids = this.cart_arr;
					obj.address_id = this.defaultAddress.id;
					obj.buyer_message = '';
					this.method = 'submitCartOrder';
				} else {
					let gid_num_sku = this.shopsMsg.gid_num_sku.split(":");
					obj.sku_id = gid_num_sku[0];
					obj.num = gid_num_sku[1];
					if (this.pickup_id) {
						obj.pick_up_id = this.pickup_id;
					} else {
						obj.address_id = this.defaultAddress.id;
					}

					obj.buy_info = gid_num_sku[0] + ":" + gid_num_sku[1];
					obj.buyer_message = '';
					this.method = 'createOrder';
					obj.shipping_type = this.shipping_type;

				}
				let addOrder = this.tui.request('order', 'post', obj, 1000, 1, '', this.method);
				Promise.resolve(addOrder).then((value) => {
					console.log(value.code)
					if (value.code == 200) {
						//删除购物车
						this.out_trade_no = value.data.out_trade_no;
						/*拉起支付*/
						this.show = true;
						this.show1 = true
					} else {
						this.tui.toast(value.message);
					}
				})
			},
			delCart() {
				let token = this.tui.getToken();
				let datas = {
					token: token,
					cart_ids: this.cart_arr
				};
				let delaCart = this.tui.request('goods', 'post', datas, 1000, 1, '', 'delCart');
				Promise.resolve(delaCart).then((value) => {

				})
			},
			popupClose() {
				this.show = false
			}
		}
	}
</script>

<style>
	.container {
		padding-bottom: 98rpx;
	}

	.tui-box {
		padding: 20rpx 0 118rpx;
		box-sizing: border-box;
	}

	.tui-address {
		min-height: 80rpx;
		padding: 10rpx 0;
		box-sizing: border-box;
		position: relative;
	}

	.tui-keyboard-tips {
		width: 100%;
		height: 120rpx;
		line-height: 1;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30rpx;
		background: #fff;
		position: relative;
		color: #333;
		border: 0;
	}

	.tui-userinfo {
		font-size: 30rpx;
		font-weight: 500;
		line-height: 30rpx;
		padding-bottom: 12rpx;
	}

	.tui-name {
		padding-right: 40rpx;
	}

	.coin-nums {
		color: #f5b82b;
		font-weight: bold;
	}

	.tui-addr {
		font-size: 24rpx;
		word-break: break-all;
		padding-right: 25rpx;
	}

	.tui-addr-tag {
		padding: 5rpx 8rpx;
		flex-shrink: 0;
		background: #EB0909;
		color: #fff;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		font-size: 25rpx;
		line-height: 25rpx;
		transform: scale(0.8);
		transform-origin: 0 center;
		border-radius: 6rpx;
	}

	.tui-bg-img {
		position: absolute;
		width: 100%;
		height: 8rpx;
		left: 0;
		bottom: 0;
		background: url("") repeat;
	}

	.tui-top {
		margin-top: 20rpx;
		overflow: hidden;
	}

	.tui-goods-title {
		font-size: 28rpx;
		display: flex;
		align-items: center;
	}

	.tui-padding {
		box-sizing: border-box;
	}

	.tui-goods-item {
		width: 100%;
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
	}

	.tui-goods-img {
		width: 180rpx;
		height: 180rpx;
		display: block;
		flex-shrink: 0;
	}

	.tui-goods-center {
		flex: 1;
		padding: 20rpx 8rpx;
		box-sizing: border-box;
	}

	.tui-goods-name {
		max-width: 310rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		font-size: 26rpx;
		line-height: 32rpx;
	}

	.tui-goods-attr {
		font-size: 22rpx;
		color: #888888;
		line-height: 32rpx;
		padding-top: 20rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	.tui-price-right {
		text-align: right;
		font-size: 24rpx;
		color: #888888;
		line-height: 30rpx;
		padding-top: 20rpx;
	}

	.tui-flex {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 26rpx;
	}

	.tui-total-flex {
		justify-content: flex-end;
	}

	.tui-color-red,
	.tui-invoice-text {
		color: #E41F19;
		padding-right: 30rpx;
	}

	.tui-balance {
		font-size: 28rpx;
		font-weight: 500;
	}

	.tui-black {
		color: #222;
		line-height: 30rpx;
	}

	.tui-gray {
		color: #888888;
		font-weight: 400;
	}

	.tui-light-dark {
		color: #666;
	}

	.tui-goods-price {
		display: flex;
		align-items: center;
		padding-top: 20rpx;
	}

	.tui-size-26 {
		font-size: 26rpx;
		line-height: 26rpx;
	}

	.tui-price-large {
		font-size: 34rpx;
		line-height: 32rpx;
		font-weight: 600;
	}

	.tui-flex-end {
		display: flex;
		align-items: flex-end;
		padding-right: 0;
	}

	.tui-phcolor {
		color: #B3B3B3;
		font-size: 26rpx;
	}

	/* #ifndef H5 */
	.tui-remark-box {
		padding: 22rpx 30rpx;
	}

	/* #endif */
	/* #ifdef H5 */
	.tui-remark-box {
		padding: 26rpx 30rpx;
	}

	/* #endif */

	.tui-remark {
		flex: 1;
		font-size: 26rpx;
		padding-left: 64rpx;
	}

	.tui-scale-small {
		transform: scale(0.8);
		transform-origin: 100% center;
	}

	.tui-scale-small .wx-switch-input {
		margin: 0 !important;
	}

	/* #ifdef H5 */
	>>>uni-switch .uni-switch-input {
		margin-right: 0 !important;
	}

	/* #endif */
	.tui-tabbar {
		width: 100%;
		height: 98rpx;
		background: #fff;
		position: fixed;
		left: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		font-size: 26rpx;
		box-shadow: 0 0 1px rgba(0, 0, 0, .3);
		padding-bottom: env(safe-area-inset-bottom);
		z-index: 996;
	}

	.tui-pr-30 {
		padding-right: 30rpx;
	}

	.tui-pr-20 {
		padding-right: 20rpx;
	}

	.tui-none-addr {
		height: 80rpx;
		padding-left: 5rpx;
		display: flex;
		align-items: center;
	}

	.tui-addr-img {
		width: 36rpx;
		height: 46rpx;
		display: block;
		margin-right: 15rpx;
	}


	.tui-pr25 {
		padding-right: 25rpx;
	}

	.tui-safe-area {
		height: 1rpx;
		padding-bottom: env(safe-area-inset-bottom);
	}
</style>